package com.example.hello.controller;

import com.example.hello.entity.DataMonitoring;
import com.example.hello.entity.OperatingParameter;
import com.example.hello.entity.DeviceInfo;
import com.example.hello.entity.AlarmInfo;
import com.example.hello.service.DeviceDetailService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class DeviceDetailController {

    @Autowired
    private DeviceDetailService deviceDetailService;

    @GetMapping("/monitoring/data")
    public ResponseEntity<Map<String, Object>> getDataMonitoring(
            @RequestParam Integer device_id,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start_time,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime end_time,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int limit) {
        
        PageInfo<DataMonitoring> pageInfo = deviceDetailService.getDataMonitoring(device_id, start_time, end_time, page, limit);
        
        Map<String, Object> response = new HashMap<>();
        response.put("status", "success");
        response.put("message", "获取设备监控数据成功");
        response.put("data", pageInfo.getList());
        response.put("total", pageInfo.getTotal());
        
        return ResponseEntity.ok(response);
    }

    @GetMapping("/parameters/operating")
    public ResponseEntity<Map<String, Object>> getOperatingParameters(
            @RequestParam Integer device_id,
            @RequestParam(required = false) String parameter,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int limit) {
        
        PageInfo<OperatingParameter> pageInfo = deviceDetailService.getOperatingParameters(device_id, parameter, page, limit);
        
        Map<String, Object> response = new HashMap<>();
        response.put("status", "success");
        response.put("message", "获取设备运行参数成功");
        response.put("data", pageInfo.getList());
        response.put("total", pageInfo.getTotal());
        
        return ResponseEntity.ok(response);
    }

    @GetMapping("/devices/info")
    public ResponseEntity<Map<String, Object>> getDeviceInfo(
            @RequestParam(required = false) Integer device_id,
            @RequestParam(required = false) String status,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int limit) {
        
        PageInfo<DeviceInfo> pageInfo = deviceDetailService.getDeviceInfo(device_id, status, page, limit);
        
        Map<String, Object> response = new HashMap<>();
        response.put("status", "success");
        response.put("message", "获取设备信息成功");
        response.put("data", pageInfo.getList());
        response.put("total", pageInfo.getTotal());
        
        return ResponseEntity.ok(response);
    }

    @GetMapping("/alarm/info")
    public ResponseEntity<Map<String, Object>> getAlarmInfo(
            @RequestParam(required = false) Integer device_id,
            @RequestParam(required = false) Boolean resolved,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start_time,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime end_time,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int limit) {
        
        PageInfo<AlarmInfo> pageInfo = deviceDetailService.getAlarmInfo(device_id, resolved, start_time, end_time, page, limit);
        
        Map<String, Object> response = new HashMap<>();
        response.put("status", "success");
        response.put("message", "获取设备报警信息成功");
        response.put("data", pageInfo.getList());
        response.put("total", pageInfo.getTotal());
        
        return ResponseEntity.ok(response);
    }
} 